<template>
  <el-card>
    <div style="float:left;margin:10px 0;">
      <el-input v-model.trim="params.keyword" placeholder="订单号/游戏账号" clearable style="width:300px;"
                @keyup.enter.native="search"></el-input>
      <el-button type="primary" @click="search">搜索</el-button>
      <el-button type="primary" @click="showAddModal" style="margin-left: 30px;">新增</el-button>
    </div>
    <el-table :data="tableData" border>
      <el-table-column label="订单编号" prop="order_no" min-width="160"></el-table-column>
      <el-table-column label="游戏账号" prop="game_account_name" min-width="180"></el-table-column>
      <el-table-column label="指定代练" prop="customer" min-width="120"></el-table-column>
      <el-table-column label="套餐名称" prop="combo_name" min-width="180"></el-table-column>
      <el-table-column label="套餐价格" prop="combo_price" min-width="180"></el-table-column>
      <el-table-column label="备注" prop="remark" min-width="260"></el-table-column>
      <el-table-column label="创建时间" prop="create_time" min-width="180"></el-table-column>
      <el-table-column label="完成时间" prop="finish_time" min-width="180"></el-table-column>
      <el-table-column label="操作" prop="op" width="300" align="center">
        <template slot-scope="scope">
          <el-button v-if="!scope.row.finish_time" type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
          <el-button v-if="!scope.row.finish_time" type="warning" size="mini" @click="copyContent(scope.row)">发推文
          </el-button>
          <el-button v-if="!scope.row.finish_time" type="success" size="mini" @click="finish(scope.row)">完成</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination layout="prev, pager, next" :current-page="params.page" :page-size="params.rows" :page-count="6"
                   :total="total" @current-change="searchPage"></el-pagination>

    <el-dialog :visible="showMergeDialog" width="450px" @close="showMergeDialog = false">
      <div slot="title" style="text-align: left;font-size: 20px;">{{opTitle}}</div>

      <el-form ref="ruleForm" label-position="left" label-width="100px" :model="formData" :rules="rules">
        <el-form-item v-if="opType != 1" label="订单编号：" prop="order_no">
          <el-input v-model.trim="formData.order_no" style="width:300px;" disabled></el-input>
        </el-form-item>
        <el-form-item label="游戏账号：" prop="game_account_id">
          <el-select v-model="formData.game_account_id" placeholder="请选择游戏账号" style="width:300px;"
                     :disabled="opType != 1">
            <el-option
              v-for="item in gameAccountList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="指定代练：" prop="customer_id">
          <el-select v-model="formData.customer_id" placeholder="请选择代练" style="width:300px;">
            <el-option
              v-for="item in customerList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="指定套餐：" prop="combo_id">
          <el-select v-model="formData.combo_id" placeholder="请选择套餐" style="width:300px;">
            <el-option
              v-for="item in comboList"
              :key="item.id"
              :label="item.title"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model.trim="formData.remark" type="textarea"
                    :rows="2" style="width:300px;" :maxlength="200" show-word-limit></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="showMergeDialog = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>

  </el-card>
</template>

<script>

    import api from '../../../utils/api';

    export default {
        name: "OrderList",
        data() {
            return {
                params: {
                    rows: 10,
                    page: 1,
                    keyword: '',
                    status: -1,
                },
                total: 0,
                tableData: [],

                showMergeDialog: false,
                customerList: [],
                comboList: [],
                gameAccountList: [],
                opType: 1,//1新增 2编辑 3详情
                opTitle: '',
                formData: {},
                rules: {
                    game_account_id: [
                        {required: true, message: '请选择游戏账号', trigger: 'change'}
                    ],
                    // customer_id: [
                    //     {required: true, message: '请选择代练', trigger: 'change'}
                    // ],
                    combo_id: [
                        {required: true, message: '请选择套餐', trigger: 'change'}
                    ],
                },
            }
        },
        methods: {
            search() {
                this.searchPage(1);
            },
            searchPage(page) {
                var that = this;
                that.params.page = page;
                api.ajax.get('/order/page/' + that.params.rows + '/' + that.params.page +
                    '?keyword=' + that.params.keyword).then((result) => {
                    var data = result.data;
                    that.tableData = data.list;
                    that.total = data.total;
                }, function (result) {
                    that.$alert("获取套餐列表异常", '失败!');
                })
            },
            showAddModal() {
                this.opType = 1;
                this.opTitle = '新增';
                this.formData = {
                    customer_id: '',
                    combo_id: '',
                    game_account_id: '',
                    remark: ''
                };
                this.showMergeDialog = true;
            },
            //获取代练列表
            getCustomerList() {
                var that = this;
                api.ajax.get('/customer/page/20/1').then((result) => {
                    var data = result.data;
                    that.customerList = data.list;
                }, function (result) {
                    that.$alert("获取客户列表异常", '失败!');
                })
            },
            //获取启用的套餐列表
            getComboList() {
                var that = this;
                api.ajax.get('/combo/page/20/1?status=1').then((result) => {
                    var data = result.data;
                    that.comboList = data.list;
                }, function (result) {
                    that.$alert("获取套餐列表异常", '失败!');
                })
            },
            //获取游戏账号列表
            getGameAccountList() {
                var that = this;
                api.ajax.get('/game/account/page/20/1').then((result) => {
                    var data = result.data;
                    that.gameAccountList = data.list;
                }, function (result) {
                    that.$alert("获取游戏账号列表异常", '失败!');
                })
            },
            edit(row) {
                this.opType = 2;
                this.opTitle = '编辑';
                this.formData = {
                    id: row.id,
                    order_no: row.order_no,
                    customer_id: row.customer_id,
                    combo_id: row.combo_id,
                    game_account_id: row.game_account_id,
                    remark: row.remark
                };
                this.showMergeDialog = true;
            },
            save() {
                var that = this;
                this.$refs["ruleForm"].validate((valid) => {
                    if (valid) {
                        if (this.opType === 1) {
                            api.ajax.post('order/add', that.formData)
                                .then((result) => {
                                    if (result.status == 200) {
                                        that.$message.success('新增成功');
                                        that.showMergeDialog = false;
                                        that.search();
                                    } else {
                                        that.$message(result.msg);
                                    }
                                }, (result) => {
                                    that.$alert("新增异常", '失败!');
                                })
                        } else {
                            api.ajax.post('order/update', that.formData)
                                .then((result) => {
                                    if (result.status == 200) {
                                        that.$message.success('修改成功');
                                        that.showMergeDialog = false;
                                        that.search();
                                    } else {
                                        that.$message(result.msg);
                                    }
                                }, (result) => {
                                    that.$alert("修改异常", '失败!');
                                })
                        }
                    } else {
                        return false;
                    }
                });
            },
            copyContent(row) {
                // 获取元素内容
                let text = "[无妄工作室-发单-" + row.order_no + "]-(" + row.combo_name + ")-(¥" + row.combo_price + ")";
                if(row.remark){
                    text+='**备注：' + row.remark;
                }
                // const text = document.getElementById(elementId).innerText;
                // 创建一个临时的输入框来存储文本
                const input = document.createElement('input');
                input.value = text;

                // 将输入框添加到页面，但不显示
                document.body.appendChild(input);
                input.select(); // 选择输入框的内容

                // 执行复制操作
                const isSuccessful = document.execCommand('copy');

                // 清除临时输入框
                document.body.removeChild(input);

                this.$message.success('已复制');
            },
            finish(row) {
                var that = this;
                let params = {
                    id: row.id,
                    customer_id: row.customer_id,
                    combo_id: row.combo_id,
                    game_account_id: row.game_account_id,
                    order_no: row.order_no
                };
                api.ajax.post('order/finish', params)
                    .then((result) => {
                        if (result.status == 200) {
                            that.$message.success('订单完成');
                            that.search();
                        } else {
                            that.$message(result.msg);
                        }
                    }, (result) => {
                        that.$alert("订单完成异常", '失败!');
                    })
            }
        },
        activated() {
            this.search();
            this.getCustomerList();
            this.getComboList();
            this.getGameAccountList();
        },
        created() {
        }
    }
</script>

<style scoped>
  .enable-status {
    padding: 5px;
    background-color: #20A0FF;
    color: white;
    border: 1px #20A0FF solid;
    border-radius: 80%;
  }

  .disable-status {
    padding: 5px;
    background-color: #F78989;
    color: white;
    border: 1px #F78989 solid;
    border-radius: 80%;
  }
</style>
